﻿/*
** Haaf's Game Engine 1.6
** Copyright (C) 2003-2006, Relish Games
** hge.relishgames.com
**
** hge_tut06 - Creating menus
*/


// Copy the files "menu.wav", "font1.fnt", "font1.png",
// "bg.png" and "cursor.png" from the folder "precompiled"
// to the folder with executable file. Also copy hge.dll
// and bass.dll to the same folder.


import core.hgeall;
import core.menuitem;
import std.c.math;
ifHge			hge;
hgeSprite 		spr;
hgeFontCN		fnt;
hgeFontCN		dfnt;  //default font

HEFFECT				snd;
HTEXTURE			tex;
hgeQuad				quad;
// Pointers to the HGE objects we will use
hgeGUI				gui;


// Pointer to the HGE interface.
// Helper classes require this to work.


// Some resource handles




extern(C) {
bool FrameFunc()
{
	float dt=hge.Timer_GetDelta();
	static float t=0.0f;
	float tx,ty;
	int id;
	static int lastid=0;

	// If ESCAPE was pressed, tell the GUI to finish
	if(hge.Input_GetKeyState(HGEK_ESCAPE)) { lastid=5; gui.Leave(); }
	
	// We update the GUI and take an action if
	// one of the menu items was selected
	id=gui.Update(dt);
	if(id == -1)
	{
		switch(lastid)
		{
			case 1:
			case 2:
			case 3:
			case 4:
				gui.SetFocus(1);
				gui.Enter();
				break;

			case 5: return true;
		}
	}
	else if(id) { lastid=id; gui.Leave(); }

	// Here we update our background animation
	t+=dt;
	tx=50*cosf(t/60);
	ty=50*sinf(t/60);

	quad.v[0].tx=tx;        quad.v[0].ty=ty;
	quad.v[1].tx=tx+800/64; quad.v[1].ty=ty;
	quad.v[2].tx=tx+800/64; quad.v[2].ty=ty+600/64;
	quad.v[3].tx=tx;        quad.v[3].ty=ty+600/64;

	return false;
}


bool RenderFunc()
{
	// Render graphics
	hge.Gfx_BeginScene();
	hge.Gfx_RenderQuad(&quad);
	gui.Render();
	fnt.SetColor(0xFFFFFFFF);
	dfnt.printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d", hge.Timer_GetDelta(), hge.Timer_GetFPS());
	dfnt.RenderVmf();
	hge.Gfx_EndScene();

	return false;
}

}		// end extern_C






void main() {
    gameInfo gi= gameInfo.getInstance();
    hge= gi.hge();

    gi.setVideo(640,480,16);
    gi.setFunc(&FrameFunc,&RenderFunc);
    gi.setStr("me","me.log");
    try {
        gi.init();

		// Load sound and textures
		quad.tex=gi.res.tex_load( r"res\bg.png");
		tex=gi.res.tex_load( r"res\cursor.png");
		snd=hge.Effect_Load(r"res\menu.wav");
		
		// Set up the quad we will use for background animation
		quad.blend=BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_NOZWRITE;

		for(int i=0;i<4;i++)
		{
			// Set up z-coordinate of vertices
			quad.v[i].z=0.5f;
			// Set up color. The format of DWORD col is 0xAARRGGBB
			quad.v[i].col=0xFFFFFFFF;
		}

		quad.v[0].x=0; quad.v[0].y=0; 
		quad.v[1].x=800; quad.v[1].y=0; 
		quad.v[2].x=800; quad.v[2].y=600; 
		quad.v[3].x=0; quad.v[3].y=600; 


		// Load the font, create the cursor sprite

		fnt =new hgeFontCN(r"res\font_cn.sbin");
		dfnt= new hgeFontCN(fnt);
		fnt.SetScale(3);
		
		spr=new hgeSprite(tex,0,0,32,32);

		// Create and initialize the GUI
		gui=new hgeGUI();

		fnt.SetScale(1.5);
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIMenuItem(1,fnt,snd,400,200,0.0f,"开始"));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIMenuItem(2,fnt,snd,400,240,0.1f,"Options"));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIMenuItem(3,fnt,snd,400,280,0.2f,"Instructions"));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIMenuItem(4,fnt,snd,400,320,0.3f,"Credits"));
		gui.AddCtrl(cast(hgeGUIObject)new hgeGUIMenuItem(5,fnt,snd,400,360,0.4f,"结束"));

		gui.SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		gui.SetCursor(spr);
		gui.SetFocus(1);
		gui.Enter();

		// Let's rock now!
		hge.System_Start();

		delete gui;
		delete spr;
		delete fnt;
		


    } catch (Exception e) {
        mes("主程异常:	",e);
    } 
	
	scope(exit){
		hge.Effect_Free(snd);
		delete gi;
	}


}

